// 股权转让
bangkeapp.controller("intereststransferCtrl", 
["$scope", "$ionicPopover", "memberService", "$state", "$ionicLoading", 
	"$ionicModal", "userinfoService", "$ionicPopup", "$ionicPopover", "fileurl",
    function($scope, $ionicPopover, memberService, $state, $ionicLoading, 
    		$ionicModal, userinfoService, $ionicPopup, $ionicPopover, fileurl) {
		
		// 查询是否支付密码, 如果设置, 跳转到支付页面, 如果没有, 跳转到设置密码页面
		memberService.issettedpaypwd().then(function(result) {
			if ("M" == result.header.ResponseCode){
				if (result.body.memberid == userinfoService.getUser().memberid
					&& "01" == result.body.handleStatus  
						&& "Y" == result.body.operateStatus 
							&& "0" == result.body.securepwdFlag) {
	
					// 打开设置密码界面
					$state.go('bangke.initpaypwd', {"inurl": "bangke.intereststransfer"});
					
		       	} else if (result.body.memberid == userinfoService.getUser().memberid
					&& "01" == result.body.handleStatus  
						&& "Y" == result.body.operateStatus 
							&& "1" == result.body.securepwdFlag) {
					
					// 转账模块
			        $ionicModal.fromTemplateUrl('interestransfernext.html', {
			            scope: $scope
			        }).then(function(modal) {
			            $scope.transemodal = modal;
			        });
			        
			        // 转账完成模块
			        $ionicModal.fromTemplateUrl('interestransferok.html', {
			            scope: $scope
			        }).then(function(modal) {
			            $scope.transeokmodal = modal;
			        });
			        
					var mypopup;
					$scope.transfer = {};
					$scope.msg = "股权将实时转入对方账号, 无法退还"
			        $scope.nextstep = function() {
			        		
			        		if (undefined == $scope.transfer.membername || "" == $scope.transfer.membername) {
			        			return false;
			        		}
						// 加载动画
						$ionicLoading.show({
							content: 'Loading',
							animation: 'fade-in',
							showBackdrop: true,
							maxWidth: 200,
							showDelay: 0
						});
						
			        		// 根据昵称查询会员
			        		memberService.getmemberinfo($scope.transfer.membername).then(function(result) {
			            		if (result.length > 0) {
			            			$scope.transfer = result[0];
			            			$scope.transfer.gragh = fileurl + $scope.transfer.gragh;
								$ionicLoading.hide();
					
								// 自定义弹窗
								mypopup = $ionicPopup.show({
									template: '<div class="content no-header ionic-pseudo writeback memberlist"'
					                    + ' style="margin: 0 auto;"><div class="list" ><a class="item item-avatar">'
					                    + '<img ng-src="{{transfer.gragh}}" style="border-radius:25px;"/>'
					                    + '<h2>{{transfer.membername}}</h2>'
					                    + '<p>{{transfer.tel}}</p></a></div></div>',
									title: '请核实要转账的会员',
									scope: $scope,
									buttons: [{
											text: '取消'
										}, {
											text: '<b>确定</b>',
											type: 'button-popup',
											onTap: function(e) {
									            $scope.transemodal.show();
											}
										}]
								});
								mypopup.then(function(res) {
									angular.element(document.querySelector('.popup-container')).remove();
								});
			            		} else {
			            			$scope.msg = "不存在该会员!";
			            		}
					        $ionicLoading.hide();
				        }, function(error) {
				            $scope.list = "error!";
				        }, function(progress) {
				
				        });
			        }
			        $scope.initinterests = {};
			        
			        $scope.insertpwd = function(){
						// 支付密码弹窗
						mypopup = $ionicPopup.show({
							template: '<input type="password" ng-model="initinterests.securepwd" '
								+ 'style="width: 80%;border: 1px solid #eee; margin: 20px auto 10px auto; height: .5rem;padding: 12px;"'
								+ ' placeholder="请输入密码" />',
							title: '请输入密码',
							scope: $scope,
							buttons: [{
									text: '取消'
								}, {
									text: '<b>确定</b>',
									type: 'button-popup',
									onTap: function(e) {
							            $scope.send();
									}
								}]
						});
						mypopup.then(function(res) {
							angular.element(document.querySelector('.popup-container')).remove();
						});
			        }
			        
					$scope.send = function(){
						
						// 加载动画
						$ionicLoading.show({
							content: 'Loading',
							animation: 'fade-in',
							showBackdrop: true,
							maxWidth: 200,
							showDelay: 0
						});
						// $scope.popover.remove();
						
						// 支付
						$scope.initinterests.srcmemberid = userinfoService.getUser().memberid;
						$scope.initinterests.tarmemberid = $scope.transfer.memberid;
						
						memberService.sendinterests($scope.initinterests).then(function(result) {
							$ionicLoading.hide();
							if ("M" == result.Response.Header.ResponseCode){
								$scope.transemodal.hide();
						        $scope.transeokmodal.show();
							} else {
								alert(result.Response.Header.MsgList[0].MsgContent);
								$scope.initinterests.securepwd = "";
							}
						}, function(error) {
							$scope.show = false;
							$scope.data = "error!";
						}, function(progress) {
							$scope.show = true;
						});
					};
					$scope.tomemberhome = function(){
						$scope.transeokmodal.hide();
						$state.go("bangke.memberhome");
					}
					$scope.goback = function(){
						$scope.transemodal.hide();
					}
				}
			} else {
				alert(result.header.MsgList[0].MsgContent);
			}
		}, function(error) {
			$scope.show = false;
			$scope.data = "error!";
		}, function(progress) {
			$scope.show = true;
		});
    }
]);


// 红包转账
bangkeapp.controller("redtransferCtrl", 
["$scope", "$ionicPopover", "memberService", "$state", "$ionicLoading", 
	"$ionicModal", "userinfoService", "$ionicPopup", "$ionicPopover", "fileurl",
    function($scope, $ionicPopover, memberService, $state, $ionicLoading, 
    		$ionicModal, userinfoService, $ionicPopup, $ionicPopover, fileurl) {
		
		// 查询是否支付密码, 如果设置, 跳转到支付页面, 如果没有, 跳转到设置密码页面
		memberService.issettedpaypwd().then(function(result) {
			if ("M" == result.header.ResponseCode){
				if (result.body.memberid == userinfoService.getUser().memberid
					&& "01" == result.body.handleStatus  
						&& "Y" == result.body.operateStatus 
							&& "0" == result.body.securepwdFlag) {
	
					// 打开设置密码界面
					$state.go('bangke.initpaypwd', {"inurl": "bangke.redtransfer"});
					
		       	} else if (result.body.memberid == userinfoService.getUser().memberid
					&& "01" == result.body.handleStatus  
						&& "Y" == result.body.operateStatus 
							&& "1" == result.body.securepwdFlag) {
			        
			        // 转账模块
			        $ionicModal.fromTemplateUrl('redtransfernext.html', {
			            scope: $scope
			        }).then(function(modal) {
			            $scope.modal = modal;
			        });
			        
			        // 转账模块
			        $ionicModal.fromTemplateUrl('redtransferok.html', {
			            scope: $scope
			        }).then(function(modal) {
			            $scope.okmodal = modal;
			        });
			        
					var mypopup;
					$scope.transfer = {};
					$scope.msg = "红包将实时转入对方账号, 无法退还"
			        $scope.nextstep = function() {
			        		
						// 加载动画
						$ionicLoading.show({
							content: 'Loading',
							animation: 'fade-in',
							showBackdrop: true,
							maxWidth: 200,
							showDelay: 0
						});
						
			        		// 根据昵称查询会员
			        		memberService.getmemberinfo($scope.transfer.membername).then(function(result) {
			            		if (result.length > 0) {
			            			$scope.transfer = result[0];
			            			$scope.transfer.gragh = fileurl + $scope.transfer.gragh;
								$ionicLoading.hide();
					
								// 自定义弹窗
								mypopup = $ionicPopup.show({
									template: '<div class="content no-header ionic-pseudo writeback memberlist"'
					                    + ' style="margin: 0 auto;"><div class="list" ><a class="item item-avatar">'
					                    + '<img ng-src="{{transfer.gragh}}" style="border-radius:25px;"/>'
					                    + '<h2>{{transfer.membername}}</h2>'
					                    + '<p>{{transfer.tel}}</p></a></div></div>',
//									template: '<div class="content no-header ionic-pseudo writeback memberlist" style="margin: 0 auto;"><div class="list" ><a class="item item-avatar">'
//					                    + '<img ng-src="{{transfer.gragh}}" /><h2>{{transfer.membername}}</h2><p>{{transfer.tel}}</p></a></div></div>',
									title: '请核实要转账的会员',
									scope: $scope,
									buttons: [{
											text: '取消'
										}, {
											text: '<b>确定</b>',
											type: 'button-popup',
											onTap: function(e) {
									            $scope.modal.show();
											}
										}]
								});
								mypopup.then(function(res) {
									angular.element(document.querySelector('.popup-container')).remove();
								});
			            		} else {
			            			$scope.msg = "不存在该会员!";
			            		}
					        $ionicLoading.hide();
				        }, function(error) {
				            $scope.list = "error!";
				        }, function(progress) {
				
				        });
			        }
			        $scope.sendredpacket = {};
			        
			        $scope.insertpwd = function(){
						// 支付密码弹窗
						mypopup = $ionicPopup.show({
							template: '<input type="password" ng-model="sendredpacket.securepwd" '
								+ 'style="width: 80%;border: 1px solid #eee; margin: 20px auto 10px auto; height: .5rem;padding: 12px;"'
								+ ' placeholder="请输入密码" />',
							title: '请输入密码',
							scope: $scope,
							buttons: [{
									text: '取消'
								}, {
									text: '<b>确定</b>',
									type: 'button-popup',
									onTap: function(e) {
							            $scope.send();
									}
								}]
						});
						mypopup.then(function(res) {
							angular.element(document.querySelector('.popup-container')).remove();
						});
						
			        }
			        
					$scope.send = function(){
						
						// 加载动画
						$ionicLoading.show({
							content: 'Loading',
							animation: 'fade-in',
							showBackdrop: true,
							maxWidth: 200,
							showDelay: 0
						});
						// $scope.popover.remove();
						
						$scope.sendredpacket.srcmemberid = userinfoService.getUser().memberid;
						$scope.sendredpacket.tarmemberid = $scope.transfer.memberid;
						
						// 发红包与红包转账区分标识
						$scope.sendredpacket.rptype = "0";
						
						memberService.sendredpacket($scope.sendredpacket).then(function(result) {
							$ionicLoading.hide();
							if ("M" == result.response.header.ResponseCode){
								$scope.modal.hide();
						        $scope.okmodal.show();
							} else {
								alert(result.response.header.MsgList[0].MsgContent);
								$scope.sendredpacket.securepwd = "";
							}
						},
						function(error) {
							$scope.show = false;
							$scope.data = "error!";
						},
						function(progress) {
							$scope.show = true;
						});
					};
					$scope.tomemberhome = function(){
						$scope.okmodal.hide();
						$state.go("bangke.memberhome");
					}
					$scope.goback = function(){
						$scope.modal.hide();
					}
			    }
			} else {
				alert(result.header.MsgList[0].MsgContent);
			}
		}, function(error) {
			$scope.show = false;
			$scope.data = "error!";
		}, function(progress) {
			$scope.show = true;
		});
	}
]);